/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-10-9
 * @Time  : 下午6:08
 * @brief :
 */
var img = __uri('./image/weixin-share.png'),
    util = require("wap_common:widget/ui/libs/util.js").util;

var Share1 = P.createUi({
    _data:{
        imgUrl:'http://'+location.host+img,
        shareTitle:"我发现了一个靠谱的理财产品，推荐给大家！",
        descContent:"赚了钱要想着我哦",
        lineLink:'http://'+location.host+'/activity/allindex/#wechat_redirect',
        appMessageTitle:"",
        appMessageDescContent:"",
        appMessageLineLink:"",
        timeLineTitle:"",
        timeLineDescContent:"",
        timeLineLineLink:"",
        appid:"wxba62c5e4899dd28a",
        isAutoGetData:true,
        onSendFriend:function(){},
        onSendFriends:function(){},
        onSend:function(){},
        onSendWeibo:function(){}
    },

    _create:function(){
        var me = this;
        document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            // 发送给好友
            WeixinJSBridge.on('menu:share:appmessage', function(argv){
                var title = me.data('appMessageTitle')?me.data('appMessageTitle'):me.data('shareTitle'),
                    desc = me.data('appMessageDescContent')?me.data('appMessageDescContent'):me.data('descContent'),
                    link =  me.data('appMessageLineLink')?me.data('appMessageLineLink'):me.data('lineLink');

                WeixinJSBridge.invoke('sendAppMessage',{
                    "appid": me.data("appid"),
                    "img_url": me.data("imgUrl"),
                    "img_width": "640",
                    "img_height": "640",
                    "link":link ,
                    "desc": desc,
                    "title":title
                }, function(res) {
                    me.trigger("sendFriend",res);
                    me.trigger("send",res);
                })
            });

            // 分享到朋友圈
            WeixinJSBridge.on('menu:share:timeline', function(argv){
                var title = me.data('timeLineTitle')?me.data('timeLineTitle'):me.data('shareTitle'),
                    desc = me.data('timeLineDescContent')?me.data('timeLineDescContent'):me.data('descContent'),
                    link =  me.data('timeLineLineLink')?me.data('timeLineLineLink'):me.data('lineLink');

                WeixinJSBridge.invoke('shareTimeline',{
                    "img_url": me.data("imgUrl"),
                    "img_width": "640",
                    "img_height": "640",
                    "link":link,
                    "desc": desc,
                    "title":title
                }, function(res) {
                    me.trigger("sendFriends",res);
                    me.trigger("send",res);
                });
            });

            //分享到微博
            function shareWeibo() {
                WeixinJSBridge.invoke('shareWeibo',{
                    "content": me.data("descContent"),
                    "url": me.data("imgUrl")
                }, function(res) {
                    me.trigger("sendWeibo",res);
                    me.trigger("send",res);
                });
            }
        }, false);
        me.trigger("beforeGetData");
        this.autoGetData();
    },

    _init:function(){
        this.on('send',function(res){

        });
    },

    //获取分享数据
    autoGetData:function(){
        var me = this,
            param = {},
            urlParam = util.getUrlParams();
        if(urlParam["user_id"]){
            param["user_id"] = urlParam["user_id"];
        }else if(urlParam["weixin_id"]){
            param["weixin_id"] = urlParam["weixin_id"];
        }
        if(me.data("isAutoGetData")){
            $.get("/activity/getsharedata",param,function(ret,status){
                me.trigger("beforeGetData");
                if(me.data("isAutoGetData")){
                    if(status == "success" && ret.error_no == 0){
                        var data = ret.data;
                        if(data.total_profit>0){
                            me.data({'shareTitle':'我在真融宝已获得￥'+data.total_profit+'收益，很靠谱，分享给大家'});
                        }else if(data.total>0){
                            me.data({'shareTitle':'我在真融宝的投资，预估一个月收益为￥'+data.month_profit+'，很靠谱，分享给大家'});
                        }else{
                            me.data({'shareTitle':'真融宝年化收益可达'+data.weight_profit_years_percent+'%，很靠谱，分享给大家'});
                        }
                        if(data.user_id){
                            me.data('lineLink','http://'+location.host+'/activity/share?user_id='+data.user_id+'#wechat_redirect');
                        }else if(data.weixin_id){
                            me.data('lineLink','http://'+location.host+'/activity/share?weixin_id='+data.weixin_id+'#wechat_redirect');
                        }
                    }
                }
            },"json");
        }
    }
});

$.extend(exports,{
    Share1:Share1,
    share1:function(option){
        return new Share1(option);
    }
});